package com.tms.project.tms.score.controller;

import java.util.Date;
import java.util.List;

import com.tms.common.utils.IpUtils;
import com.tms.common.utils.security.ShiroUtils;
import com.tms.project.tms.score.domain.ScoreDetailVO;
import com.tms.project.tms.score.domain.ScoreVO;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.tms.framework.aspectj.lang.annotation.Log;
import com.tms.framework.aspectj.lang.enums.BusinessType;
import com.tms.project.tms.score.domain.Score;
import com.tms.project.tms.score.service.IScoreService;
import com.tms.framework.web.controller.BaseController;
import com.tms.framework.web.domain.AjaxResult;
import com.tms.common.utils.poi.ExcelUtil;
import com.tms.framework.web.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;

/**
 * tmsController
 *
 * @author xyl
 * @date 2021-03-24
 */
@Controller
@RequestMapping("/tms/score")
public class ScoreController extends BaseController {
    private String prefix = "tms/score";

    @Autowired
    private IScoreService scoreService;

    @RequiresPermissions("tms:score:view")
    @GetMapping("type1")
    public String score1() {
        return prefix + "/score1";
    }
    @RequiresPermissions("tms:score:view")
    @GetMapping("type2")
    public String score2() {
        return prefix + "/score2";
    }


    @RequiresPermissions("tms:score:detail")
    @GetMapping("/detail1/{teacherRecordId}")
    public String detail1(@PathVariable("teacherRecordId")Integer teacherRecordId,ModelMap mmap) {
        List<ScoreDetailVO> list = scoreService.selectScoreDetailVoList(1,teacherRecordId);
        mmap.put("scores",list);
        return prefix + "/detail1";
    }

    @RequiresPermissions("tms:score:detail")
    @GetMapping("/detail2/{teacherRecordId}")
    public String detail2(@PathVariable("teacherRecordId") Integer teacherRecordId,ModelMap mmap) {
        List<ScoreDetailVO> list = scoreService.selectScoreDetailVoList(2,teacherRecordId);
        mmap.put("scores",list);
        return prefix + "/detail2";
    }

    /**
     * 查询tms列表
     */
    @RequiresPermissions("tms:score:list1")
    @PostMapping("/list1")
    @ResponseBody
    public TableDataInfo list1(Score score) {
        startPage();
        score.setType(1);
        List<ScoreVO> list = scoreService.selectScoreVOList(score);
        return getDataTable(list);
    }
    /**
     * 查询tms列表
     */
    @RequiresPermissions("tms:score:list2")
    @PostMapping("/list2")
    @ResponseBody
    public TableDataInfo list2(Score score) {
        startPage();
        score.setType(2);
        List<ScoreVO> list = scoreService.selectScoreVOList(score);
        return getDataTable(list);
    }

    /**
     * 导出tms列表
     */
    @RequiresPermissions("tms:score:export")
    @Log(title = "tms", businessType = BusinessType.EXPORT)
    @PostMapping("/export1")
    @ResponseBody
    public AjaxResult export1(Score score) {
        score.setType(1);
        List<ScoreVO> list = scoreService.selectScoreVOList(score);
        ExcelUtil<ScoreVO> util = new ExcelUtil<>(ScoreVO.class);
        return util.exportExcel(list, "score");
    }
    /**
     * 导出tms列表
     */
    @RequiresPermissions("tms:score:export")
    @Log(title = "tms", businessType = BusinessType.EXPORT)
    @PostMapping("/export2")
    @ResponseBody
    public AjaxResult export2(Score score) {
        score.setType(2);
        List<ScoreVO> list = scoreService.selectScoreVOList(score);
        ExcelUtil<ScoreVO> util = new ExcelUtil<>(ScoreVO.class);
        return util.exportExcel(list, "score");
    }

    /**
     * 新增tms
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存tms
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Score score, HttpServletRequest request) {
        List<Score> repeat;
        if (1 == score.getType()) {
            //查询有无重复提交的
            score.setIpAddress(IpUtils.getIpAddr(request));
            repeat = scoreService.selectScoreRepeat(score);
        }else if(2 == score.getType()){
            if(StringUtils.isEmpty(ShiroUtils.getLoginName())){
                return AjaxResult.error("非法提交!");
            }
            score.setCreateBy(ShiroUtils.getLoginName());
            repeat = scoreService.selectScoreRepeat(score);
        }else{
            return AjaxResult.error("非法提交!");
        }
        if(null != repeat && repeat.size() > 0){
            return AjaxResult.error("不得重复提交!");
        }
        return toAjax(scoreService.insertScore(score));
    }

    /**
     * 修改tms
     */
    @GetMapping("/edit/{scoreId}")
    public String edit(@PathVariable("scoreId") Long scoreId, ModelMap mmap) {
        Score score = scoreService.selectScoreById(scoreId);
        mmap.put("score", score);
        return prefix + "/edit";
    }

    /**
     * 修改保存tms
     */
    @RequiresPermissions("tms:score:edit")
    @Log(title = "tms", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Score score) {
        return toAjax(scoreService.updateScore(score));
    }

    /**
     * 删除tms
     */
    @RequiresPermissions("tms:score:remove")
    @Log(title = "tms", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(scoreService.deleteScoreByIds(ids));
    }
}
